package com.itzb.asone.admin.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itzb.asone.admin.constant.AdminConstant;
import com.itzb.asone.admin.constant.AdminMessageConstant;
import com.itzb.asone.application.BaseController;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysApi;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysApiService;
import com.itzb.asone.system.service.ISysDictService;
import com.itzb.asone.utils.GitId;
import com.itzb.asone.utils.JsonResult;
import com.itzb.asone.utils.PageResult;

@Controller
@RequestMapping(value = "sysapi")
public class SysApiController extends BaseController {
	
	private Logger logger = Logger.getLogger(getClass()); 
	
	@Autowired
	private IMessageService messageService;
	@Autowired
	private ISysApiService sysApiService;
	@Autowired
	private ISysDictService sysDictService;

	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public String list(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		return "system/sysapi/list";
	}

	@ResponseBody
	@RequestMapping(value = "/getdata", method = { RequestMethod.POST })
	public PageResult getData(
			@RequestParam(required = false, defaultValue = "0", value = "offset") int page,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int pageSize,
			@RequestParam(required = false, value = "order") String order,
			@RequestParam(required = false, value = "sort") String sort,
			HttpServletRequest request,HttpServletResponse response
			) {
		PageResult pageResult = new PageResult();	
		Criteria<SysApi> param = new Criteria<SysApi>();
		param.setPageSize(pageSize).setStartIndex(page);
		param.setOrderBy(sort + " " + order);
		List<SysApi> list = sysApiService.queryPage(param);
		pageResult.setTotal(sysApiService.queryPageCount(param));
		pageResult.setRows(sysDictService.translateToMapList(list));
		return pageResult;
	}

	@RequestMapping(value = "/showmodify", method = { RequestMethod.GET })
	public String showModify(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) {
		String id = request.getParameter("id");
		SysApi sysApi = new SysApi();
		if(id != null && !id.equals("")){
			sysApi = sysApiService.queryEntityById(id);
		}
		modelMap.put("sysApi", sysApi);
		return "system/sysapi/modify";
	}

	@ResponseBody
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public JsonResult modify(SysApi sysApi,HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		String showtype = request.getParameter("showtype");
		Criteria<SysApi> param = new Criteria<SysApi>();
		if(showtype.equals(AdminConstant.SHOW_TYPE_UPDATE)){
			param.addParam("id", sysApi.getId());
			param.setRecord(sysApi);
			sysApiService.updateByCriteria(param);
		} else {
			sysApi.setCreateTime(new Date());
			param.clearParams();
			param.addParam("apiCode", sysApi.getApiCode());
			if(sysApiService.queryPage(param).size() > 0){
				return jsonResult.declareFailure(messageService.message(AdminMessageConstant.SYSTEM_ADD_ERROR_3));
			}
			sysApi.setId(GitId.primaryKey());
			sysApiService.insert(sysApi);
		}
		jsonResult.declareSuccess();
		return jsonResult;
	}
	
	@ResponseBody
	@RequestMapping(value = "/del", method = { RequestMethod.POST })
	public JsonResult del(HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		String[] ids = request.getParameterValues("id[]");
		for(int i=0;i<ids.length;i++){
			sysApiService.deleteById(ids[i]);
			
		}
		jsonResult.declareSuccess();
		return jsonResult;
	}

}
